package com.sjm.suchbear.minibookmall.action;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sjm.suchbear.minibookmall.model.CarModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Suchbear on 2018/5/5.
 */
public class CarManage {
    //根据用户ID获取购物车
    public static List<CarModel> getCarbyid(SQLiteDatabase db, String userID) {
        List<CarModel> carModels = new ArrayList<>();
        Cursor cursor = db.query("mall_car", new String[]{"bookID", "bookname", "num", "price"}, "userID=?"
                , new String[]{userID}, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                CarModel carModel = new CarModel();
                carModel.setBookID(cursor.getString(0));
                carModel.setBookname(cursor.getString(1));
                carModel.setNum(cursor.getInt(2));
                carModel.setPrice(cursor.getString(3));
                carModels.add(carModel);
            }
            return carModels;
        }
        cursor.close();
        return null;
    }

    public static CarModel getCarBookbyid(SQLiteDatabase db, String userID, String bookID) {
        CarModel carModel = new CarModel();
        Cursor cursor = db.query("mall_car", new String[]{"bookID", "bookname", "num", "price"}, "userID=? and bookID=?"
                , new String[]{userID, bookID}, null, null, null);
        if (cursor.getCount() > 0) {
            cursor.moveToNext();
            carModel.setBookID(cursor.getString(0));
            carModel.setBookname(cursor.getString(1));
            carModel.setNum(cursor.getInt(2));
            carModel.setPrice(cursor.getString(3));
            return carModel;
        }
        cursor.close();
        return null;
    }

    //新增购物车
    public static void addCar(SQLiteDatabase db, String userID, String bookID, String bookname, String price) {
        ContentValues values = new ContentValues();
        values.put("userID", userID);
        values.put("bookID", bookID);
        values.put("bookname", bookname);
        values.put("num", 1);
        values.put("price", price);
        db.insert("mall_car", null, values);
    }

    //修改购物车数量
    public static void changeCarNumbyBook(SQLiteDatabase db, String userID, String bookID, int num) {
        ContentValues values = new ContentValues();
        values.put("num", num);
        db.update("mall_car", values, "userID=? and bookID=?", new String[]{userID, bookID});
    }


    //根据用户和书本删除购物车
    public static void deleteCarByBook(SQLiteDatabase db, String userID, String bookID) {
        db.delete("mall_car", "userID=? and bookID=?", new String[]{userID, bookID});
    }

    //根据书本删除所有用户购物车
    public static void deleteCarByBook(SQLiteDatabase db, String bookID) {
        db.delete("mall_car", "bookID=?", new String[]{bookID});
    }

    //根据用户清空购物车
    public static void deleteCarByuser(SQLiteDatabase db, String userID) {
        db.delete("mall_car", "userID=?", new String[]{userID});
    }
}
